package com.zmovie.front.controller;

import com.alibaba.fastjson.JSON;
import com.zmovie.common.utils.FileUtils;
import com.zmovie.framework.domain.Movie;
import com.zmovie.framework.domain.MovieRate;
import com.zmovie.framework.domain.dto.MovieRateDTO;
import com.zmovie.framework.service.MovieRateService;
import com.zmovie.framework.service.MovieService;
import com.zmovie.front.constant.Constants;
import com.zmovie.front.controller.vo.MovieVO;
import com.zmovie.front.util.MoviePathUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 挚爱之夕
 * @version 1.0
 * @implSpec 前台首页
 * @since 2023-12-12 11:28
 */
@Controller
@RequestMapping("/")
public class IndexController {
    @Autowired
    private MovieService movieService;
    @Autowired
    private MovieRateService movieRateService;
    @GetMapping
    public String index(Model model){
        //正在热映
        List<Movie> onScreenMovies = movieService.listMovieOnScreen(Constants.SHOW_ITEM_CNT);
        model.addAttribute(Constants.ON_SCREEN_MOVIES, convert(onScreenMovies));
        //即将热映
        List<Movie> upComingMovies = movieService.listMovieUpcoming(Constants.SHOW_ITEM_CNT);
        model.addAttribute(Constants.UP_COMING_MOVIES, convert(upComingMovies));
        //经典影片
        List<Movie> classicalMovies = movieService.listMovieClassical(Constants.SHOW_ITEM_CNT);
        model.addAttribute(Constants.CLASSICAL_MOVIES, convert(classicalMovies));
        //top100
        List<MovieRateDTO> topMovies = movieRateService.listTop(Constants.SHOW_ITEM_CNT);
        model.addAttribute(Constants.TOP_MOVIES, topMovies);
        return "index";
    }
    private List<MovieVO> convert(List<Movie> movies){
        return movies.stream().map(this::convert).collect(Collectors.toList());
    }
    private MovieVO convert(Movie movie){
        //处理文件路径
        MovieVO movieVO = MoviePathUtil.dealMovieFilePath(movie);
        //评分
        MovieRate movieRate = movieRateService.getByMovieId(movie.getId());
        movieVO.setMovieRate(movieRate);
        return movieVO;
    }
}
